package edu.hcnu.controller;

import edu.hcnu.ApiResult;
import edu.hcnu.entity.Car;
import edu.hcnu.entity.CarRecord;
import edu.hcnu.service.TicketService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: Administrator
 * @time: 2023/4/29 18:05
 */
@Api(tags = "车票接口")
@RestController
@RequestMapping("/ticket")
class TicketController {


    @Autowired
    private TicketService service;

    /**
     * 查询剩余车票信息
     *
     * @param cid cid
     * @return {@link ApiResult}
     */
    @ApiOperation(value = "查询剩余车票信息")
    @GetMapping("query/{cid}")
    ApiResult query(@PathVariable("cid") String cid) {

        String res = service.query(cid);

        if(!"没有这个车次".equals(res)){
            return ApiResult.success(200, "查询剩余车票信息成功", res);
        }
        return ApiResult.fail(400,res);
    }
    /**
     * 获得所有的购票记录
     *

     * @return {@link ApiResult}
     */
    @ApiOperation(value = "获得所有的购票记录")
    @GetMapping("all")
    ApiResult getAll() {

        List<CarRecord> res = service.getAll();
        return ApiResult.success(200, "获得所有的购票记录成功", res);
    }

    /**
     * 查询我的车票信息
     *
     * @param uid uid
     * @return {@link ApiResult}
     */
    @ApiOperation(value = "查询我的车票信息")
    @GetMapping("query/my/{uid}")
    ApiResult queryMyTicket(@PathVariable("uid")String uid){
        List<Car> list=service.queryMyTicketInfo(uid);
        return ApiResult.success(200,"查询我的车票信息成功",list);
    }

    /**
     * 买车票
     *
     * @param cid cid
     * @param uid uid
     * @return {@link ApiResult}
     */
    @ApiOperation(value = "购买车票")
    @PutMapping("buy/{cid}/{uid}")
    ApiResult buy(@PathVariable("cid") String cid, @PathVariable("uid") String uid) {
        String res = service.buy(cid, uid);
        if ("success".equals(res)) {
            return ApiResult.success(200, res, true);
        }
        return ApiResult.fail(400, res);
    }

    /**
     * 用户退票
     *
     * @param cid cid
     * @param uid uid
     * @return {@link ApiResult}
     */
    @ApiOperation(value = "用户退票")
    @DeleteMapping("delete/{cid}/{uid}")
    ApiResult deleteTicket(@PathVariable("cid") String cid, @PathVariable("uid") String uid){
        boolean res = service.delete(cid, uid);
        if (res) {
            return ApiResult.success(200, "退票成功", true);

        }
        return ApiResult.fail(400, "退票失败");
    }
}
